<template>
  <div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="18"
        ><span style="font-weight: blod"></span>工艺参数预设</el-col
      >
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-delete"
          size="mini"
          :disabled="multipleSet"
          @click="handleDeleteSelect"
          >删除</el-button
        >
      </el-col>
    </el-row>
    <el-table :cell-style="{ padding: this.tablePadding }" :data="selectItems" @selection-change="handleSelectionChangeSet">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        v-for="(item, index) in tableHead"
        :key="index"
        :label="item.equipmentParam"
        :prop="item.collectColumn"
        align="center"
        min-width="120px"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row[item.collectColumn]"
            placeholder="请输入值"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { removeByValue } from "@/utils/index";
import { getEquipmentParam } from "@/api/equipment/param";
export default {
  props: ["selectItems",'id'],
  data() {
    return {
      multiple: true,
      multipleSet: true,
      open: false,
      ids: [],
      initId: 100000,
      tableHead: [],
    };
  },
  watch:{
      id(newValue){
          if(!newValue){
              return
          }
          this.getEquipmentParam(newValue)
      }
  },
  methods: {
    getEquipmentParam(id) {
      getEquipmentParam(id).then((res) => {
          this.tableHead=res.data
      });
    },
    handleAdd(){
        let obj={}
        this.tableHead.map(item=>{ 
            obj[item.collectColumn]=''
            obj.id=++this.initId
        })
        this.selectItems.push(obj)
    },
    handleDelete(id) {
      removeByValue(this.selectItems, "id", id);
    },
    handleDeleteSelect() {
      this.ids.forEach((item) => {
        removeByValue(this.selectItems, "id", item);
      });
    },
    cancel() {
      this.open = false;
      this.selectItems = [];
    },
    handleSelectionChangeSet(selection) {
      this.ids = selection.map((item) => item.id);

      this.multipleSet = !selection.length;
    },
  },
};
</script>

<style>
</style>